package org.mch.func.m_px.sorteg.handle;

import javax.swing.JButton;

import org.mch.comm.exec.MsgException;
import org.mch.func.m_px.sorteg.SortPanel;
import org.mch.func.m_px.sorteg.utils.MoveComponentGoBtnSort;
import org.mch.swingm.core.MToggleButton;

/**
 * 描述：移动按钮；<BR/>
 * 
 * @author MCH；Date：2008-06-18；<BR/>
 *
 */
public class HandleExchangeBtn extends AbstractLCSXEGHandle {

	public HandleExchangeBtn(SortPanel panel) {
		super(panel);
	}

	public void onHandle(JButton btn) throws Exception {
		MToggleButton[] btnsorts = getPanel().getBtnSorts();
		MToggleButton btn1 = null;
		MToggleButton btn2 = null;

		if (btnsorts == null || btnsorts.length <= 0) {
			throw new MsgException("请先构造出排序按钮！");
		}

		for (MToggleButton b : btnsorts) {
			if (btn1 == null && b.isSelected()) {
				btn1 = b;
			} else if (btn2 == null && b.isSelected()) {
				btn2 = b;
			}
		}

		moveTwoButton(btn1, btn2);
	}

	protected void moveTwoButton(final MToggleButton btn1, final MToggleButton btn2) throws Exception {
		if (btn1 == null || btn2 == null) {
			throw new MsgException("请选择要交换的两个按钮！");
		}
		final int x1 = btn1.getX();
		final int y1 = btn1.getY();
		final int x2 = btn2.getX();
		final int y2 = btn2.getY();

		// btn1.setLocation(x2, y2);
		// btn2.setLocation(x1, y1);

		// getPanel().getPanelUp().updateUI();

		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					new MoveComponentGoBtnSort().moveButton(btn1, x1, y1 + 100, getPanel().getPanelUp());

					new Thread(new Runnable() {
						@Override
						public void run() {
							try {
								new MoveComponentGoBtnSort().moveButton(btn2, x2, y2 + 50, getPanel().getPanelUp());

								new Thread(new Runnable() {
									@Override
									public void run() {
										try {
											new MoveComponentGoBtnSort().moveButton(btn1, x2, y1 + 100, getPanel().getPanelUp());

											new Thread(new Runnable() {
												@Override
												public void run() {
													try {
														new MoveComponentGoBtnSort().moveButton(btn2, x1, y2 + 50, getPanel()
																.getPanelUp());

														new Thread(new Runnable() {
															@Override
															public void run() {
																try {
																	new MoveComponentGoBtnSort().moveButton(btn1, x2, y2, getPanel()
																			.getPanelUp());

																	new Thread(new Runnable() {
																		@Override
																		public void run() {
																			try {
																				new MoveComponentGoBtnSort().moveButton(btn2, x1, y1,
																						getPanel().getPanelUp());
																			} catch (Exception e) {
																				e.printStackTrace();
																			}
																		}
																	}).start();
																} catch (Exception e) {
																	e.printStackTrace();
																}
															}
														}).start();
													} catch (Exception e) {
														e.printStackTrace();
													}
												}
											}).start();
										} catch (Exception e) {
											e.printStackTrace();
										}
									}
								}).start();
							} catch (Exception e) {
								e.printStackTrace();
							}
						}
					}).start();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}).start();
	}

}
